package com.ruoyi.system.vo;

import java.util.Map;
import java.util.logging.Logger;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

import com.ruoyi.system.vo.weishuapay.DreBean;
import com.ruoyi.system.vo.weishuapay.DreBean.PayReturnBean;
import com.ruoyi.system.vo.weishuapay.DreBean.QueryBalanceBean;
import com.ruoyi.system.vo.weishuapay.DreBean.QueryBalanceReturnBean;
import com.ruoyi.system.vo.weishuapay.DreBean.RegisterReturnBean;
import com.ruoyi.system.vo.weishuapay.DreBean.WithDrawBean;
import com.ruoyi.system.vo.weishuapay.DreBean.WithDrawReturnBean;
import com.ruoyi.system.vo.weishuapay.KuaijieBean;
import com.ruoyi.system.vo.weishuapay.KuaijieReturnBean;
import com.ruoyi.system.vo.weishuapay.PayCodeBean;
import com.ruoyi.system.vo.weishuapay.SignUtil;



public class TransUtil {
	
	private static Logger logger4J = Logger.getLogger(TransUtil.class
			.getSimpleName());


	static ObjectMapper mapper = new ObjectMapper();
	static {
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.setSerializationInclusion(Inclusion.NON_NULL);
	}

	@SuppressWarnings({ "unchecked", "static-access" })
	public byte[] packet(CommonBean trans,String signkey) throws Exception {
		if (signkey != null) {
			String resp = this.object2String(trans);
			logger4J.info("===weishuapay准备上诉的数据，未加密===="+resp);
			Map<String, String> resMap = mapper.readValue(resp, Map.class);
			String sign = SignUtil.getSign(resMap, signkey);
			trans.setSign(sign);
		}
		byte[] returnData = this.object2Byte(trans);
		return returnData;
	}
	@SuppressWarnings({ "unchecked", "static-access" })
	public byte[] packet(DreBean.RegisterBean trans,String signkey) throws Exception {
		if (signkey != null) {
			String resp = this.object2String(trans);
			logger4J.info("@@@weishua商户入驻"+resp);
			Map<String, String> resMap = mapper.readValue(resp, Map.class);
			String sign = SignUtil.getSign(resMap, signkey);
			trans.setSign(sign);
		}
		byte[] returnData = this.object2Byte(trans);
		return returnData;
	}
	@SuppressWarnings({ "unchecked", "static-access" })
	public byte[] packet(WithDrawBean trans,String signkey) throws Exception {
		if (signkey != null) {
			String resp = this.object2String(trans);
			logger4J.info("@@@weishua大额代付"+resp);
			Map<String, String> resMap = mapper.readValue(resp, Map.class);
			String sign = SignUtil.getSign(resMap, signkey);
			trans.setSign(sign);
		}
		byte[] returnData = this.object2Byte(trans);
		return returnData;
	}
	@SuppressWarnings({ "unchecked", "static-access" })
	public byte[] packet(QueryBalanceBean trans,String signkey) throws Exception {
		if (signkey != null) {
			String resp = this.object2String(trans);
			logger4J.info("@@@weishua大额查询余额"+resp);
			Map<String, String> resMap = mapper.readValue(resp, Map.class);
			String sign = SignUtil.getSign(resMap, signkey);
			trans.setSign(sign);
		}
		byte[] returnData = this.object2Byte(trans);
		return returnData;
	}
	@SuppressWarnings({ "unchecked", "static-access" })
	public byte[] packet(DreBean.PayBean trans,String signkey) throws Exception {
		if (signkey != null) {
			String resp = this.object2String(trans);
			logger4J.info("@@@weishua大额消费"+resp);
			Map<String, String> resMap = mapper.readValue(resp, Map.class);
			String sign = SignUtil.getSign(resMap, signkey);
			trans.setSign(sign);
		}
		byte[] returnData = this.object2Byte(trans);
		return returnData;
	}

	@SuppressWarnings({ "unchecked", "static-access" })
	public byte[] packet(KuaijieBean trans,String signkey) throws Exception {
		if (signkey != null) {
			String resp = this.object2String(trans);
			logger4J.info("===weishuapay-快捷支付准备上诉的数据，未加密===="+resp);
			Map<String, String> resMap = mapper.readValue(resp, Map.class);
			String sign = SignUtil.getSign(resMap, signkey);
			trans.setSign(sign);
		}
		byte[] returnData = this.object2Byte(trans);
		return returnData;
	}

	public CommonBean json2Trans(byte[] json) {
		CommonBean trans = null;
		try {
			trans = (CommonBean) mapper.readValue(json, CommonBean.class);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
			e.printStackTrace();
		}
		return trans;
	}

	public CommonBean json2Trans(String json) {
		CommonBean trans = null;
		try {
			trans = (CommonBean) mapper.readValue(json, CommonBean.class);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
			e.printStackTrace();
		}
		return trans;
	}

	public KuaijieReturnBean json2Kuaijie(String json) {
		KuaijieReturnBean trans = null;
		try {
			trans = (KuaijieReturnBean) mapper.readValue(json, KuaijieReturnBean.class);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
		}
		return trans;
	}
	
	public PayCodeBean json2PayCode(String json) {
		PayCodeBean trans = null;
		try {
			trans = (PayCodeBean) mapper.readValue(json, PayCodeBean.class);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
		}
		return trans;
	}
	

	public String object2json(Object obj) throws Exception {
		if (obj != null) {
			return mapper.writeValueAsString(obj);
		}
		return null;
	}

	public byte[] object2Byte(Object object) {
		byte[] response = null;
		try {
			response = mapper.writeValueAsBytes(object);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
			e.printStackTrace();
		}
		return response;
	}

	public static String object2String(Object o) {
		String response = null;
		try {
			if (null != o) {
				response = mapper.writeValueAsString(o);
			}
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
			e.printStackTrace();
		}

		return response;
	}
	
	public static WithDrawReturnBean json2withDrawBean(String json) {
		WithDrawReturnBean trans = null;
		try {
			trans = (WithDrawReturnBean) mapper.readValue(json, WithDrawReturnBean.class);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
		}
		return trans;
	}
	public static PayReturnBean json2PayReturnBean(String json) {
		PayReturnBean trans = null;
		try {
			trans = (PayReturnBean) mapper.readValue(json, PayReturnBean.class);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
		}
		return trans;
	}
	public static QueryBalanceReturnBean json2queryBalance(String json) {
		QueryBalanceReturnBean trans = null;
		try {
			trans = (QueryBalanceReturnBean) mapper.readValue(json, QueryBalanceReturnBean.class);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
		}
		return trans;
	}

	public static RegisterReturnBean json2RegisterReturnBean(String json) {
		RegisterReturnBean trans = null;
		try {
			trans = (RegisterReturnBean) mapper.readValue(json, RegisterReturnBean.class);
		} catch (Exception e) {
			logger4J.info("ERROR:"+e.getMessage());
		}
		return trans;
	}
}
